{% extends 'base/setting_base.html' %}

{% block body %}
    <script type="text/javascript">

        $(document).ready(function () {
            $('.btn-outline-success').click(function () {
                $.ajax({
                    type: 'POST',
                    <!--contentType: 'application/json; charset=utf-8',-->
                    url: '{% url 'archeryapi:access-key' %}',
                    data: {
                        name: $('input[name=name]').val(),
                        csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val()
                    },
                    sucess: function () {

                    }
                });
                setTimeout(function(){location.reload();},1000);
            });

        });

        $(document).ready(function() {
        $('.btn-danger').click(function(){
            if (confirm('Are you sure you want to delete this?')) {
                var uu_id = ['a7af5b68-b661-45ad-90fd-ffa27526cdce'];
                $.each($("input[name='del_scan_id']:checked"), function(){
                    uu_id.push($(this).val());
                });

                var json_uu_id = uu_id.join(", ");
                $.ajax({
                    type:'POST',
                    <!--contentType: 'application/json; charset=utf-8',-->
                    url:'{% url 'archeryapi:access-key-delete' %}',
                    data:{
                       uu_id: json_uu_id,
                       csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val()
                    },
                    sucess:function(){

                    }
                });
                setTimeout(function(){location.reload();},1000);
            }
        });

    });

        $(document).on('click', '.confirm-delete', function () {
            return confirm('Are you sure you want to delete this?');
        })

    </script>

    <div class="row">
        <div class="col-md-12">
            <div class="card">
                <div class="card-body">
                    <div class="float-right">
                        {% if user.is_superuser == True or user.role.id == 2 %}
                            <button type="button" class="btn btn-primary" data-toggle="modal"
                                    data-target="#exampleModalCenter">
                                <i class="fas fa-plus-circle"></i> Add
                            </button>

                            <button type="button" class="btn btn-danger btn-mini" onclick="">Delete Selected</button>
                        {% endif %}
                    </div>
                    <div class="d-md-flex align-items-center">
                        <h1 class="card-title">Access Keys</h1>
                    </div>
                    <br>
                    <div class="row">
                        <div class="table-responsive">
                            <table id="data_table" class="table table-bordered data-table">
                                <thead>
                                <tr>
                                <th><label class="customcheckbox m-b-20">
                                    <input type="checkbox" id="mainCheckbox">
                                    <span class="checkmark"></span>
                                </label></th>
                                    <th>Name</th>
                                    <th>Date Time</th>
                                    <th>Key</th>
                                </tr>
                                </thead>
                                <tbody>{% for data in all_active_keys %}
                                    <tr>
                                        <td>
                                            <form>
                                                <label class="customcheckbox">
                                                    <input type="checkbox" class="listCheckbox"
                                                           value="{{ data.uu_id }}"
                                                           name="del_scan_id"
                                                           onClick="reload">
                                                    <span class="checkmark"></span>
                                                </label>
                                            </form>
                                        </td>
                                        <td>{{ data.name }}</td>
                                        <td>{{ data.created_time }}</td>
                                        <td>{{ data.api_key }}</td>
                                    </tr>
                                {% endfor %}
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>


    <!-- Modal -->
    <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog"
         aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLongTitle">Add Key</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form>{% csrf_token %}
                        <div class="form-group">
                            <div class="form-group row">
                                <div class="col-sm-4">
                                    <label class="col-sm-4 col-form-label">Name</label>
                                </div>
                                <div class="col-sm-6">
                                    <input type="text" name="name" class="form-control" placeholder="API Key Name"/>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-outline-success">Save changes</button>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
